package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.entity.AddressBook;
import com.itheima.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @PackageName: com.itheima.controller
 * @ClassName: AddressBookController
 * @Description:
 * @Author: zhangcheng
 * @DateTime: 2022/6/12 18:49
 * @Version 1.0
 */

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook) {
        //关联当前用户
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success("保存地址成功");
    }

    /**
     * 设置默认地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<String> defaultAddress(@RequestBody AddressBook addressBook) {
        //更新is_default为0
        addressBookService.update(
                new LambdaUpdateWrapper<AddressBook>()
                        .set(AddressBook::getIsDefault, 0)
                        .eq(AddressBook::getUserId, BaseContext.getCurrentId()));
        //设置当前设置的地址的is_default为1
        addressBookService.update(
                new LambdaUpdateWrapper<AddressBook>()
                        .set(AddressBook::getIsDefault, 1)
                        .eq(AddressBook::getId, addressBook.getId()));
        return R.success("设置成功");
    }

    /**
     * 根据id查询地址
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> getAddressById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);

        if (addressBook != null) {
            return R.success(addressBook);
        } else {
            return R.error("没有找到该对象");
        }
    }

    /**
     * 查询默认地址
     *
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefaultAddress() {
        // 当前id关联的默认地址(两个条件)
        AddressBook address = addressBookService.getOne(
                new LambdaQueryWrapper<AddressBook>()
                        .eq(AddressBook::getUserId, BaseContext.getCurrentId())
                        .eq(AddressBook::getIsDefault, 1));
        if (address == null) {
            return R.error("没有默认地址");
        }
        return R.success(address);
    }

    /**
     * 查询指定用户的全部地址
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> getAddressWithUser() {
        List<AddressBook> list = addressBookService.list(
                new LambdaQueryWrapper<AddressBook>()
                        .eq(AddressBook::getUserId, BaseContext.getCurrentId()));
        return R.success(list);
    }

    /**
     * 删除地址
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        addressBookService.removeByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 修改地址
     *
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook) {
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }


}
